/**
 * 
 */
package de.cbf.cam.locations.model.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;

import de.cbf.cam.locations.model.entities.ModelLocations;
import de.cbf.cam.locations.model.interfaces.ILocation;
import de.cbf.cam.locations.model.ui.CompositeLocationTree;

/**
 * @author Stefan Alter
 *
 */
public class ControllerLocations {	
	private ModelLocations	 		model = new ModelLocations();
	private CompositeLocationTree 	view;
	
	/**
	 * 
	 */
	public ControllerLocations() {		
		model.getPropertyChangeSupport().addPropertyChangeListener(pclModel);
	}
	
	public boolean addToRootLocations(ILocation value){	
		return model.addToRootLocations(value);
	}
	public boolean removeFromRootLocations(ILocation value) {
		return model.removeFromRootLocations(value);
	}
	public boolean removeFromRootLocations(String locationName) {
		return model.removeFromRootLocations(locationName);
	}
	
	/**
	 * @return currently selected location or <null> if none is selected
	 */
	public ILocation getSelectedLocation(){
		if(view != null){
			return view.getSelectedLocation();
		}
		return null;
	}
	
	/**
	 * @param value the view to set
	 */
	public void setView(CompositeLocationTree value) {
		if(view != null){
			if(view.equals(value)){
				return;
			}else{
				deRegisterViewListeners(view);				
			}			
		}
		view = value;	
		view.setLocations(model.iteratorRootLocations());
		registerViewListerners(view);		
	}
	
	protected void registerViewListerners(CompositeLocationTree value) {
		value.registerLocationSelectedListener(locationSelection);		
	}

	protected void deRegisterViewListeners(CompositeLocationTree value) {
		value.deRegisterLocationSelectedListener(locationSelection);		
	}

	public void setViewParent(Composite parent,GridData layoutData){
		if(parent == null)return;
		CompositeLocationTree newView = new CompositeLocationTree(parent, SWT.NONE);
		if(layoutData != null){
			newView.setLayoutData(layoutData);
		}else{
			newView.setLayoutData(new GridData(GridData.FILL_BOTH));
		}		
		setView(newView);		
	}	
	
	public void setLocations(Collection<? extends ILocation> values){
		model.setRootLocations(values);
	}
	
	private Collection<ActionListener> locationSelectionListeners = new ArrayList<ActionListener>();
	public boolean registerLocationSelectionListener(ActionListener listener){
		return locationSelectionListeners.add(listener);
	}
	public boolean deRegisterLocationSelectionListener(ActionListener listener){
		return locationSelectionListeners.remove(listener);
	}
	private LocationSelectionAdvancer locationSelection = new LocationSelectionAdvancer();	
	private class LocationSelectionAdvancer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ControllerLocations.this, ActionEvent.ACTION_PERFORMED, "LocationSelection:"+e.getActionCommand());
			for (ActionListener listener : locationSelectionListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	private PCLModel pclModel = new PCLModel();
	private class PCLModel implements PropertyChangeListener {
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			if(view != null){
				view.setLocations(model.iteratorRootLocations());
			}			
		}		
	}
}
